package com.roberto.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.roberto.main.Main;

public class Invader extends Entity {
    public int invPoints;
    public static final Sprite invExplosion;
    private float separatorX;
    private float separatorY;
    private Sprite[] sprite = new Sprite[2];
    private static final Sound invKilled;
    private static final Sound[] invMove = new Sound[4];
    private int invId;
    private static int soundID = 1;
    private int current = 0;

    static {
        invKilled = Gdx.audio.newSound(Gdx.files.internal("res/sounds/invaderkilled16.wav"));
        invMove[0] = Gdx.audio.newSound(Gdx.files.internal("res/sounds/sound1.wav"));
        invMove[1] = Gdx.audio.newSound(Gdx.files.internal("res/sounds/sound2.wav"));
        invMove[2] = Gdx.audio.newSound(Gdx.files.internal("res/sounds/sound3.wav"));
        invMove[3] = Gdx.audio.newSound(Gdx.files.internal("res/sounds/sound4.wav"));
        invExplosion = new Sprite(Main.megaTexture, 1, 170, 52, 32);
    }

    public Invader(int x, int y) {
        invId = y;
        loadTexture(invId);
        width = sprite[current].getWidth();
        height = sprite[current].getHeight();
        separatorX += width * 1.3f;
        separatorY = height * 1.9f;
        this.x += 170 + x * separatorX;
        this.y = Main.HEIGHT - 110 - y * separatorY;
        destroyed = false;
        sprite[current].setPosition(this.x, this.y);

    }

    @Override
    public void update(float delta) {
        sprite[current].setPosition(x, y);
    }

    public void move(float dir, boolean down) {
        if (down) {
            y -= Math.abs(dir);
        } else {
            x += dir;
        }
        current = current == 0 ? 1 : 0;

    }

    private void loadTexture(int invaderId) {
        switch (invaderId) {
        case 1:
            sprite[0] = new Sprite(Main.megaTexture, 1, 60, 32, 32);
            sprite[1] = new Sprite(Main.megaTexture, 38, 60, 32, 32);
            this.x = 9f;
            separatorX = 20.7f;
            invPoints = 40;
            break;
        case 2:
        case 3:
            sprite[0] = new Sprite(Main.megaTexture, 2, 98, 44, 32);
            sprite[1] = new Sprite(Main.megaTexture, 49, 98, 44, 32);
            this.x = 3;
            separatorX = 5.1f;
            invPoints = 20;
            break;
        default:
            sprite[0] = new Sprite(Main.megaTexture, 1, 135, 48, 32);
            sprite[1] = new Sprite(Main.megaTexture, 54, 135, 48, 32);
            invPoints = 10;
            break;
        }

    }

    public static void playSound() {
        switch (soundID) {
        case 1:
            invMove[0].play(0.2f);
            soundID++;

            break;
        case 2:
            invMove[1].play(0.2f);
            soundID++;
            break;
        case 3:
            invMove[2].play(0.2f);
            soundID++;
            break;
        default:
        case 4:
            invMove[3].play(0.2f);
            soundID = 1;
            break;
        }
    }

    public void destroy() {
        Invader.invExplosion.setPosition(x, y);
        Invader.invExplosion.setSize(width, height);
        destroyed = true;
        invKilled.play(0.2f);
    }

    public final Sprite getSprite() {
        return sprite[current];
    }

    public final Rectangle getBounds() {
        return sprite[current].getBoundingRectangle();
    }

    public final int getID() {
        return invId;
    }

}
